﻿using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Xps.Packaging;
using Poet.Views;
using PrintDialog = System.Windows.Controls.PrintDialog;

namespace Poet.Modules
{
    public static class PrintModule
    {
        private static readonly PrintDialog PrintDialog =
            new PrintDialog
                {
                    PageRangeSelection = PageRangeSelection.AllPages,
                    UserPageRangeEnabled = true
                };

        private static readonly PageSetupDialog PageSetupDialog =
            new PageSetupDialog
                {
                    PageSettings = new PageSettings()
                };

        public static void Print(FlowDocument flowDocument, string title)
        {
            flowDocument.PagePadding =
                           new Thickness(PageSetupDialog.PageSettings.Margins.Left * 0.39,
                                         PageSetupDialog.PageSettings.Margins.Top * 0.39,
                                         PageSetupDialog.PageSettings.Margins.Right * 0.39,
                                         PageSetupDialog.PageSettings.Margins.Bottom * 0.39);

            PrintDialog.PrintTicket.PageOrientation =
                PageSetupDialog.PageSettings.Landscape
                    ? PageOrientation.Landscape
                    : PageOrientation.Portrait;

            //var paperSize = PageSetupDialog.PageSettings.PaperSize;            
            //PrintDialog.PrintTicket.PageMediaSize = //new PageMediaSize(PageMediaSizeName.Roll18Inch);
            //  new PageMediaSize(paperSize.Width, paperSize.Height);
            //PrintDialog.PrintTicket.PageMediaSize.PageMediaSizeName = paperSize.PaperName;          
            //  PrintDialog.PrintTicket.PageMediaType = Enum.PageSetupDialog.PageSettings.PaperSource.ToString());
            IDocumentPaginatorSource idpSource = flowDocument;
            if (PrintDialog.ShowDialog() == true)
                PrintDialog.PrintDocument(idpSource.DocumentPaginator, title);
        }

        public static void ShowPageSetupDialog()
        {
            if (PageSetupDialog.ShowDialog() != DialogResult.OK) return;
            PageSetupDialog.PageSettings.Margins.Left =
                PageSetupDialog.PageSettings.Margins.Left * 100 / 39;
            PageSetupDialog.PageSettings.Margins.Right =
                PageSetupDialog.PageSettings.Margins.Right * 100 / 39;
            PageSetupDialog.PageSettings.Margins.Top =
                PageSetupDialog.PageSettings.Margins.Top * 100 / 39;
            PageSetupDialog.PageSettings.Margins.Bottom =
                PageSetupDialog.PageSettings.Margins.Bottom * 100 / 39;
        }

        public static void ShowPrintPreviewDialog(FlowDocument flowDocument, string title)
        {
            if (PageSetupDialog.PageSettings.Landscape)
            {
                flowDocument.PageWidth = PageSetupDialog.PageSettings.PaperSize.Height;
                flowDocument.ColumnWidth = PageSetupDialog.PageSettings.PaperSize.Height;
                flowDocument.PageHeight = PageSetupDialog.PageSettings.PaperSize.Width;
            }
            else
            {
                flowDocument.PageWidth = PageSetupDialog.PageSettings.PaperSize.Width;
                flowDocument.ColumnWidth = PageSetupDialog.PageSettings.PaperSize.Width;
                flowDocument.PageHeight = PageSetupDialog.PageSettings.PaperSize.Height;
            }

            flowDocument.PagePadding =
                new Thickness(PageSetupDialog.PageSettings.Margins.Left * 0.39,
                              PageSetupDialog.PageSettings.Margins.Top * 0.39,
                              PageSetupDialog.PageSettings.Margins.Right * 0.39,
                              PageSetupDialog.PageSettings.Margins.Bottom * 0.39);

            ShowPrintPreviewDialog((IDocumentPaginatorSource)flowDocument, title);
        }

        private static void ShowPrintPreviewDialog(IDocumentPaginatorSource flowDocument, string title)
        {
            var fileName = Path.GetRandomFileName();
            File.Create(fileName).Dispose();

            using (var document = new XpsDocument(fileName, FileAccess.Write))
            {
                var writer = XpsDocument.CreateXpsDocumentWriter(document);
                writer.Write(flowDocument.DocumentPaginator);
            }

            using (var document = new XpsDocument(fileName, FileAccess.ReadWrite))
            {
                var sequence = document.GetFixedDocumentSequence();
                new PrintPreviewView {DocumentViewer = {Document = sequence}, Title = title}.ShowDialog();
            }

            File.Delete(fileName);
        }
    }
}
